package com.jch.lx;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class Test2 {

    public static void main(String[] args) throws Exception{

       //创建流处理环境
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        //数据源
        DataSource<String> source = env.readTextFile("D:\\ideawenjian\\1019\\data\\data.txt");

        AggregateOperator<Tuple2<String, Integer>> flatMap = source.flatMap(new FlapFunction()).groupBy(0).sum(1);

        flatMap.print("flatmap");

        env.execute();


    }



    //自定义FlatMapFunction
    public static class FlapFunction implements FlatMapFunction<String,Tuple2<String,Integer>>{

        @Override
        public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
            String[] split = s.split("\t");

            for(String ss:split){
                collector.collect(new Tuple2<>(ss,1));
            }

        }
    }


}




